/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package JET.mygui;

import JET.CenterPanels.CPScene;
import JET.Centers.ControlCenter;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import org.w3c.dom.*;

/**
 *
 * @author 057Ry
 */
public class CenterTabsPane extends TabsPane implements ContainerListener {

    // This CC reference shouldnt be here
    ControlCenter CC = ControlCenter.getInstance();

    /** Availables tabs types */
    public enum TabType {
        Normal,
        JavaScriptEditor,
        GraphicPanel
    }

    /**
     * When component wants to inform center tabs pane of its type it should
     * implement this interface.
     * This is used for determination of what icon should be drawn on
     * tabs pane header.
     */
    public interface CenterTabsPaneTab extends TabsPaneTab{
        TabType getTabType();
        String getTitle();
    }

    protected static BufferedImage tabActiveLeftImg, tabActiveCenterImg, tabActiveRightImg,
                                   tabActiveNofocusLeftImg, tabActiveNofocusCenterImg, tabActiveNofocusRightImg,
                                   tabNormalLeftImg, tabNormalCenterImg, tabNormalRightImg,
                                   tabNormalMouseOverLeftImg, tabNormalMouseOverCenterImg, tabNormalMouseOverRightImg,
                                   tabIconNormal, tabIconJS, tabIconGP;

    public CenterTabsPane(String name) {
        super(name);
        header = createNewTabsPaneHeader();
        setHeader(header);

        // Updates header state when new comp is added...
        addContainerListener(this);

        /*
        addTabsPaneMyFocusListener( new TabsPaneMyFocusListener() {
            public void focusGained(TabsPane tp, int tabIndex) {
                Component tab = tp.getActualComponent();
                if (tab instanceof CPScene) {
                    CC.GUIC.setActualScene( ((CPScene)tab).getSceneName() );
                    System.out.println("actual scene is : " + ((CPScene)tabs.get(tabIndex)).getSceneName());
                }
            }
        });
        */

        // Load static images
        try {
            // Active tab
            tabActiveLeftImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_active_left.png"));
            tabActiveCenterImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_active_center.png"));
            tabActiveRightImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_active_right.png"));
            // Active without of focus tab
            tabActiveNofocusLeftImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_active_nofocus_left.png"));
            tabActiveNofocusCenterImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_active_nofocus_center.png"));
            tabActiveNofocusRightImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_active_nofocus_right.png"));
            // Normal tab
            tabNormalLeftImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_normal_left.png"));
            tabNormalCenterImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_normal_center.png"));
            tabNormalRightImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_normal_right.png"));
            // Normal tab with mouse focus
            tabNormalMouseOverLeftImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_normal_mouseover_left.png"));
            tabNormalMouseOverCenterImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_normal_mouseover_center.png"));
            tabNormalMouseOverRightImg = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_normal_mouseover_right.png"));

            tabIconNormal = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_icon_normal.png"));
            tabIconJS = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_icon_js.png"));
            tabIconGP = ImageIO.read(CenterTabsPane.this.getClass().getResource("images/tab_icon_gp.png"));
        } catch (IOException e) { e.printStackTrace(); }
    }

    @Override
    protected TabsPaneHeader createNewTabsPaneHeader() {
        return new CenterTabsPaneHeader();
    }
    
    ////////////////////////////////////////////////////////////////////////
    //              C O N T A I N E R   L I S T E N E R                   //
    ////////////////////////////////////////////////////////////////////////

    public void componentAdded(ContainerEvent e) {
        ((CenterTabsPaneHeader)header).updateSizes();
    }

    public void componentRemoved(ContainerEvent e) {
        ((CenterTabsPaneHeader)header).updateSizes();
    }

    ////////////////////////////////////////////////////////////////////////////
    //                         X M L   S T U F F                              //
    ////////////////////////////////////////////////////////////////////////////

    @Override
    public Element getXMLElement(Document doc) {
        Element root = doc.createElement("centerTabsPane");

        // All tabs should be CenterTabsPaneTab istances
        for(JComponent comp : tabs) {
            CenterTabsPaneTab tab = (CenterTabsPaneTab) MyGuiHelper.getComponentAfterScrollPane(comp);
            Element el = doc.createElement("tab");
            el.setAttribute("type", "center");
            el.setAttribute("id", ""+tab.getTabType());
            if (tab.getTabType() == TabType.JavaScriptEditor)
                el.setAttribute("title", tab.getTitle());

            root.appendChild(el);
        }

        return root;
    }

    ////////////////////////////////////////////////////////////////////////////
    //                      H E A D E R   C L A S S                           //
    ////////////////////////////////////////////////////////////////////////////

    protected class CenterTabsPaneHeader extends TabsPane.TabsPaneHeader implements ImageObserver {

        protected int[]     sizes;
        protected Graphics  graphics;
        protected int       marginLeft  = 35,
                            marginRight = 30;
        ArrayList<JButton>  closeButtons;

        public CenterTabsPaneHeader() {
            super();
            closeButtons = new ArrayList<JButton>();
        }
        @Override
        public int getTabIndexAt(Point p) {
            if (isEmpty() || sizes==null)
                return -1;

            for(int i=0; i<sizes.length; i++) {
                    int x1  = (int)( MyGuiHelper.getSumOfNElements(sizes, i) ),
                        x2  = (int)(x1 + sizes[i]);
                    if (p.getX()>=x1 && p.getX()<=x2)
                        return i;
            }
            return -1;
        }

        protected int calcTabWidth(int index, Graphics g) {
            String name = getTabName(index);
            return (int)(MyGuiHelper.getTextSize(name, g).getWidth() + marginLeft + marginRight);
        }

        /**
         * Calculates tabs hearders sizes according to their name.
         *
         * @param g Graphics object calculation string width for actual font.
         */
        protected void updateSizes() {
            if (this.graphics!=null) {
                sizes = new int[tabs.size()];
                for (int i=0; i<tabs.size(); i++)
                    sizes[i] = calcTabWidth(i, this.graphics);
            }
        }

        @Override
        public void paintComponent(Graphics g){

            if (g == null)
                 return;
            //super.paintComponent(g);
            this.graphics = g;

            if (sizes == null)
                updateSizes();

            Graphics2D g2d = (Graphics2D)g;
            if (tabs.size()>0)
                for(int i=0; i<tabs.size(); i++) {
                    // Tab body
                    int x1      = (int)( MyGuiHelper.getSumOfNElements(sizes, i) ),
                        y1      = (int) 0,
                        x2      = (int)(x1 + sizes[i]),
                        y2      = (int)(getHeight());
                    
                    if (i==actualTabIndex) {
                        if (hasFocusFromTabs) {
                            g.drawImage(tabActiveLeftImg, x1, y1, x1+2, y2, 0, 0, tabActiveLeftImg.getWidth(), tabActiveLeftImg.getHeight(), this);
                            g.drawImage(tabActiveCenterImg, x1+2, y1, x2-2, y2, 0, 0, tabActiveCenterImg.getWidth(), tabActiveCenterImg.getHeight(), this);
                            g.drawImage(tabActiveRightImg, x2-2, y1, x2, y2, 0, 0, tabActiveRightImg.getWidth(), tabActiveRightImg.getHeight(), this);
                        } else {
                            g.drawImage(tabActiveNofocusLeftImg, x1, y1, x1+2, y2, 0, 0, tabActiveNofocusLeftImg.getWidth(), tabActiveNofocusLeftImg.getHeight(), this);
                            g.drawImage(tabActiveNofocusCenterImg, x1+2, y1, x2-2, y2, 0, 0, tabActiveNofocusCenterImg.getWidth(), tabActiveNofocusCenterImg.getHeight(), this);
                            g.drawImage(tabActiveNofocusRightImg, x2-2, y1, x2, y2, 0, 0, tabActiveNofocusRightImg.getWidth(), tabActiveNofocusRightImg.getHeight(), this);
                        }
                    } else {
                        if (i==mouseOverIndex) {
                            g.drawImage(tabNormalMouseOverLeftImg, x1, y1, x1+2, y2, 0, 0, tabNormalMouseOverLeftImg.getWidth(), tabNormalMouseOverLeftImg.getHeight(), this);
                            g.drawImage(tabNormalMouseOverCenterImg, x1+2, y1, x2-2, y2, 0, 0, tabNormalMouseOverCenterImg.getWidth(), tabNormalMouseOverCenterImg.getHeight(), this);
                            g.drawImage(tabNormalMouseOverRightImg, x2-2, y1, x2, y2, 0, 0, tabNormalRightImg.getWidth(), tabNormalRightImg.getHeight(), this);
                        }
                        else {
                            g.drawImage(tabNormalLeftImg, x1, y1, x1+2, y2, 0, 0, tabNormalLeftImg.getWidth(), tabNormalLeftImg.getHeight(), this);
                            g.drawImage(tabNormalCenterImg, x1+2, y1, x2-2, y2, 0, 0, tabNormalCenterImg.getWidth(), tabNormalCenterImg.getHeight(), this);
                            g.drawImage(tabNormalRightImg, x2-2, y1, x2, y2, 0, 0, tabNormalRightImg.getWidth(), tabNormalRightImg.getHeight(), this);
                        }
                    }

                    // Tab icon
                    BufferedImage img = null;
                    JComponent comp = (JComponent) MyGuiHelper.getComponentAfterScrollPane(tabs.get(i));
                    if (comp instanceof CenterTabsPaneTab)
                        switch ( ((CenterTabsPaneTab) comp).getTabType() ) {
                            case GraphicPanel : img = tabIconGP; break;
                            case JavaScriptEditor : img = tabIconJS; break;
                            case Normal : img = tabIconNormal; break;
                            default: img = tabIconNormal; break;
                        }
                    else img = tabIconNormal;

                    g.drawImage(img, x1+6, (getHeight()-img.getHeight())/2+1, this);

                    // Tab name
                    String name = getTabName(i);
                    if (name==null) name = "No name";
                    Dimension textD = MyGuiHelper.getTextSize(name, g);
                    int ty = getHeight()-(int)((getHeight()-textD.getHeight())/2.)-3;
                    
                    g2d.setColor(Color.BLACK);
                    g2d.drawString(getTabName(i), x1+marginLeft, ty);
                }
            else {
                g.setColor(new Color(127, 157, 185));
                g2d.draw( new Rectangle2D.Double(0, 0,getWidth()-1, getHeight()-1));
                g2d.drawString("_empty_",0,getHeight()-5);
            }
        }

        ////////////////////////////////////////////////////////////////////////
        //                     D R A G   A N D   D R O P                      //
        ////////////////////////////////////////////////////////////////////////

        @Override
        public boolean accepts(Object o) {
            Component comp = MyGuiHelper.getComponentAfterScrollPane((JComponent) o);
            return comp instanceof CenterTabsPaneTab;
        }
    }
}